package com.zushiye.order.controller;


import com.zushiye.order.model.UserInfo;
import com.zushiye.order.model.response.BaseResponse;
import com.zushiye.order.model.response.LoginResponse;
import com.zushiye.order.service.UserInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;


@RestController
@RequestMapping("/user_info")
public class UserInfoController {

    @Autowired
    private UserInfoService service;

    //http://127.0.0.1:8080/user_info/login
    @PostMapping(value = "/login")
    public LoginResponse login(@RequestBody UserInfo info) {
        LoginResponse response = new LoginResponse();
        boolean a = StringUtils.isEmpty(info.user_name);
        boolean b = StringUtils.isEmpty(info.pwd);
        if (a) {
            response.msg = "账号不能为空";
            return response;
        } else if (b) {
            response.msg = "密码不能为空";
            return response;
        }

        UserInfo userInfo = service.getUser(info.user_name);

        if (userInfo == null) {
            response.msg = "还未注册";
        } else if (userInfo.pwd.equalsIgnoreCase(info.pwd)) {
            response.msg = "登录成功";
            response.code = 1;
            response.userInfo = userInfo;
        } else {
            response.msg = "密码错误";
        }

        return response;
    }


    @PostMapping(value = "/regist")
    public BaseResponse regist(@RequestBody UserInfo info) {
        BaseResponse response = new BaseResponse();
        boolean a = StringUtils.isEmpty(info.user_name);
        boolean b = StringUtils.isEmpty(info.pwd);
        if (a) {
            response.msg = "账号不能为空";
            return response;
        } else if (b) {
            response.msg = "密码不能为空";
            return response;
        }
        int count = service.getUserCount(info.user_name);
        if (count > 0) {
            service.deleteUser(info.user_name);
        }
        service.insert(info);
        response.msg = "注册成功";
        response.code = 1;
        return response;
    }

}
